package com.nhc.activity_fragment;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.provider.Settings;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.astrob.nhcmap.R;
import com.astrob.util.SysUtils;
import com.astrob.view.BaseViewHolder;
import com.nhc.database.dbmodel.TrackPoint;

import java.util.ArrayList;
import java.util.List;

import zuo.biao.library.base.BaseActivity;
import zuo.biao.library.base.BaseAdapter;
import zuo.biao.library.model.Entry;
import zuo.biao.library.ui.PageScroller;

public class SystemSettingActivity extends BaseActivity {
    private final String TAG = "SystemSettingActivity";

    //启动方法<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<


    /**
     * 启动这个Activity的Intent
     *
     * @param context
     * @return
     */
    public static Intent createIntent(Context context) {
        return new Intent(context, SystemSettingActivity.class);
    }
    //启动方法>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

    @Override
    public Activity getActivity() {
        return this;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.systemsetting_activity);

        //功能归类分区方法，必须调用<<<<<<<<<<
        initView();
        initData();
        initEvent();
        //功能归类分区方法，必须调用>>>>>>>>>>

    }


    //UI显示区(操作UI，但不存在数据获取或处理代码，也不存在事件监听代码)<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    //示例代码<<<<<<<<
    private ListView lv_setting;

    //示例代码>>>>>>>>
    @Override
    public void initView() {//必须在onCreate方法内调用
        autoSetTitle();//自动设置标题为上个Activity传入的INTENT_TITLE

        //示例代码<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
        lv_setting = (ListView) findViewById(R.id.lv_setting);
        //示例代码>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
    }

    //示例代码<<<<<<<<
    private DemoAdapter adapter;
    //示例代码>>>>>>>>

    /**
     * 示例方法 ：显示列表内容
     *
     * @param list
     */
    private void setList(List<Entry<Integer, String>> list) {
        if (adapter == null) {
            adapter = new DemoAdapter(context);
            lv_setting.setAdapter(adapter);
        }
        adapter.refresh(list);
    }
    //UI显示区(操作UI，但不存在数据获取或处理代码，也不存在事件监听代码)>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


    //Data数据区(存在数据获取或处理代码，但不存在事件监听代码)<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    //示例代码<<<<<<<<
    private List<Entry<Integer, String>> searchtypelist;

        private final int[] SETTING_IDS_240x320 = {
            R.drawable.settings_settings, R.drawable.settings_map, R.drawable.settings_function,
            R.drawable.settings_save, R.drawable.settings_unit, R.drawable.settings_coordinates,
            R.drawable.settings_direction, R.drawable.settings_altimeter, R.drawable.settings_track,
            R.drawable.settings_about};

    private final int[] SETTING_IDS = {
            R.drawable.settings_map, R.drawable.settings_function,
            R.drawable.settings_save, R.drawable.settings_unit, R.drawable.settings_coordinates,
            R.drawable.settings_direction, R.drawable.settings_altimeter, R.drawable.settings_track,
            R.drawable.settings_about};

    //示例代码>>>>>>>>>
    @Override
    public void initData() {//必须在onCreate方法内调用
        //示例代码<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
        showProgressDialog(R.string.loading);
        runThread(TAG + "initData", new Runnable() {
            @Override
            public void run() {
                searchtypelist = getList();
                runUiThread(new Runnable() {
                    @Override
                    public void run() {
                        dismissProgressDialog();
                        setList(searchtypelist);
                    }
                });
            }
        });
        //示例代码>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
    }

    protected List<Entry<Integer, String>> getList() {
        if (SysUtils.getInstance().is240x320) {
            String[] SETTING_NAMES = getResources().getStringArray(R.array.syssetting_names2_240x320);
            searchtypelist = new ArrayList<Entry<Integer, String>>();
            for (int i = 0; i < SETTING_NAMES.length; i++) {
                searchtypelist.add(new Entry<Integer, String>(i, ""));
            }
        } else {
            String[] SETTING_NAMES = getResources().getStringArray(R.array.syssetting_names2);
            searchtypelist = new ArrayList<Entry<Integer, String>>();
            for (int i = 0; i < SETTING_NAMES.length; i++) {
                searchtypelist.add(new Entry<Integer, String>(i, ""));
            }
        }


        return searchtypelist;
    }

    //Data数据区(存在数据获取或处理代码，但不存在事件监听代码)>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


    //Event事件区(只要存在事件监听代码就是)<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    @Override
    public void initEvent() {//必须在onCreate方法内调用
        //示例代码<<<<<<<<<<<<<<<<<<<
        lv_setting.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (SysUtils.getInstance().is240x320) {
                    onToNext240x320(position);
                } else {
                    onToNext(position);
                }

            }
        });

        //分页滚动示例代码<<<<<<<<<<<<<<<<<<<
        new PageScroller(lv_setting).init();
        //分页滚动示例代码>>>>>>>>>>>>>>>>>>>

        //示例代码>>>>>>>>>>>>>>>>>>>
    }

    private void onToNext240x320(int pos) {
        switch (pos) {
            default:
                break;
            case 0: {
//                toActivity(SysSetActivity.createIntent(this), 0);
                Intent i = new Intent(Settings.ACTION_SETTINGS);
                startActivityForResult(i, 0);
            }
            break;
            case 1:
                toActivity(MapSettingActivity.createIntent(this), 1);
                break;
            case 2:
                toActivity(NaviTrackSettingActivity.createIntent(this), 2);
                break;
            case 3:
                toActivity(PoiCollectionActivity.createIntent(this), 3);
                break;
            case 4:
                toActivity(UnitActivity.createIntent(this), 4);
                break;
            case 5:
                toActivity(CoordSetActivity.createIntent(this), 5);
                break;
            case 6:
                toActivity(CourseActivity.createIntent(this), 6);
                break;
            case 7:
                toActivity(SensorSetActivity.createIntent(this), 7);
                break;
            case 8:
                toActivity(NaviSettingActivity.createIntent(this), 8);
                break;
            case 9:
                toActivity(AboutActivity.createIntent(this), 9);
                break;
        }
    }

    private void onToNext(int pos) {
        switch (pos) {
            default:
                break;
            case 0:
                toActivity(MapSettingActivity.createIntent(this), 1);
                break;
            case 1:
                toActivity(NaviTrackSettingActivity.createIntent(this), 2);
                break;
            case 2:
                toActivity(PoiCollectionActivity.createIntent(this), 3);
                break;
            case 3:
                toActivity(UnitActivity.createIntent(this), 4);
                break;
            case 4:
                toActivity(CoordSetActivity.createIntent(this), 5);
                break;
            case 5:
                toActivity(CourseActivity.createIntent(this), 6);
                break;
            case 6:
                toActivity(SensorSetActivity.createIntent(this), 7);
                break;
            case 7:
                toActivity(NaviSettingActivity.createIntent(this), 8);
                break;
            case 8:
                toActivity(AboutActivity.createIntent(this), 9);
                break;
        }

    }

    //系统自带监听方法<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    //@user 新增方法

    @Override
    public void finish() {
        super.finish();
    }

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        switch (keyCode) {
            case KeyEvent.KEYCODE_BACK:
                break;
        }
        return super.onKeyUp(keyCode, event);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode != RESULT_OK) {
            return;
        }
    }


    //系统自带监听方法>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
    //Event事件区(只要存在事件监听代码就是)>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


    //Adapter
    public class DemoAdapter extends BaseAdapter<Entry<Integer, String>> {
        public DemoAdapter(Activity context) {
            super(context);
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = LayoutInflater.from(context).inflate(
                        R.layout.icontext_view, parent, false);
            }

            TextView tv_name = BaseViewHolder.get(convertView, R.id.tv_name);
            ImageView iv_icon = BaseViewHolder.get(convertView, R.id.iv_icon);

            if (!SysUtils.getInstance().is240x320) {
                final Entry<Integer, String> kvb = getItem(position);
                int index = kvb.getKey();
                if (index < 0 || index >= SETTING_IDS.length) {
                    index = 0;
                }
                String[] SETTING_NAMES = getResources().getStringArray(R.array.syssetting_names2);
                tv_name.setText(SETTING_NAMES[index]);
                iv_icon.setImageResource(SETTING_IDS[index]);
            } else {
                final Entry<Integer, String> kvb = getItem(position);
                int index = kvb.getKey();
                if (index < 0 || index >= SETTING_IDS_240x320.length) {
                    index = 0;
                }
                String[] SETTING_NAMES = getResources().getStringArray(R.array.syssetting_names2_240x320);
                tv_name.setText(SETTING_NAMES[index]);
                iv_icon.setImageResource(SETTING_IDS_240x320[index]);
            }

            return super.getView(position, convertView, parent);
        }

        private final long serialVersionUID = 3L;

        @Override
        public long getItemId(int position) {
//            return getItem(position).getId();
            return serialVersionUID + position;
        }
    }

}
